import pandas as pd
import matplotlib.pyplot as plt

plt.rcParams['font.sans-serif'] = 'SimHei'
plt.rcParams['axes.unicode_minus'] = False
plt.figure(figsize=(12, 8))

def areaView(df):
	counts = df.groupby(by="区域").count()
	plt.title("各个区域的房源数量分析")
	plt.bar(counts.index, counts["标题"], label="数量")
	plt.xticks(rotation=45)
	plt.ylabel("数量")
	plt.legend()
	plt.show()


def housePlotView(df):
	counts = df.groupby(by="户型").count()  # 统计
	plt.title("各种户型数量分析")
	plt.plot(counts.index, counts["标题"], label="数量")
	plt.xticks(rotation=45)
	plt.ylabel("数量")
	plt.legend()
	plt.show()


def houseBarView(df):
	counts = df["户型"].value_counts().sort_index()
	plt.bar(counts.index, counts.values)
	plt.xlabel("户型")
	plt.ylabel("数量")
	plt.title("户型统计")
	plt.xticks(rotation=45)
	plt.grid(axis="y")
	plt.show()

def doubleView(df):
	df["单价"] = pd.to_numeric(df["单价"], errors='coerce')
	df["总价"] = pd.to_numeric(df["总价"], errors='coerce')
	counts = df["单价"].groupby(df["区域"]).median()  # 统计
	fig = plt.figure()
	ax1 = fig.add_subplot(111)
	ax1.bar(counts.index, counts.values, label="平均单价（元/平米）")
	ax2 = ax1.twinx()
	counts = df["总价"].groupby(df["区域"]).median()  # 统计
	ax2.plot(counts.index, counts.values, label="总价（万）", color="red")
	fig.legend(loc=1, bbox_to_anchor=(1, 1), bbox_transform=ax1.transAxes)  # 防止图例变形
	plt.show()


def areaPieView(df):
	# 分割面积数据为不同的区间
	divide = [0, 70, 90, 120, 140, 160, 1200]
	# 获取面积列数据
	cut = pd.cut(list(df["面积"]), divide)
	data = cut.describe()
	# 绘制扇形图
	plt.pie(x=data.freqs, labels=data.index, autopct="%1.2f%%", startangle=45, radius=1)
	plt.legend(loc='best')
	plt.title("面积占比分析")
	plt.show()


def likeView(df):
	df = df.drop(df[df["关注"] == -1].index)
	res = df["关注"].groupby(df["区域"]).sum()
	plt.title("各个区域关注人数统计")
	plt.xticks(rotation=45)
	plt.bar(res.index, res.values)
	plt.show()
